﻿using System.Runtime.InteropServices;
using System;

namespace PeapodCatcher
{
    public class IdleTime
    {
        [DllImport("User32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO lastInput);

        [StructLayout(LayoutKind.Sequential)]
        public struct LASTINPUTINFO
        {
            public int cbSize;
            public int dwTime;
        }

        public int GetLastUserInput()
        {
            LASTINPUTINFO lastInput = new LASTINPUTINFO();
            lastInput.cbSize = Marshal.SizeOf(lastInput);
            if (GetLastInputInfo(ref lastInput))
            {
                return (Environment.TickCount - lastInput.dwTime) / 1000;
            }
            return -1;
        }
    }
}